package com.luugiathuy.apps.remotebluetooth;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.nio.ByteBuffer;

import javax.microedition.io.StreamConnection;

public class ProcessConnectionThread implements Runnable {

    private StreamConnection mConnection;

    // Constant that indicate command from devices
    private static final int EXIT_CMD = -1;
    private static final int KEY_RIGHT = 1;
    private static final int KEY_LEFT = 2;

    public ProcessConnectionThread(StreamConnection connection) {
	mConnection = connection;
    }

    @Override
    public void run() {

	try {
	    byte[] position = new byte[4];
	    InputStream inputStream = mConnection.openInputStream();
	    while (true) {
		// prepare to receive data
		inputStream.read(position, 0, 4);
		System.out.println(ByteBuffer.wrap(position).getInt());
	    }
	    // System.out.println("waiting for input");
	    //
	    // while (true) {
	    // int command = inputStream.read();
	    // System.out.println(command);
	    //
	    // if (command == EXIT_CMD) {
	    // System.out.println("finish process");
	    // break;
	    // }
	    //
	    // processCommand(command);
	    // }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Process the command from client
     * 
     * @param command
     *            the command code
     */
    private void processCommand(int command) {
	try {
	    Robot robot = new Robot();
	    switch (command) {
	    case KEY_RIGHT:
		robot.keyPress(KeyEvent.VK_RIGHT);
		System.out.println("Right");
		// release the key after it is pressed. Otherwise the event just
		// keeps getting trigged
		robot.keyRelease(KeyEvent.VK_RIGHT);
		break;
	    case KEY_LEFT:
		robot.keyPress(KeyEvent.VK_LEFT);
		System.out.println("Left");
		// release the key after it is pressed. Otherwise the event just
		// keeps getting trigged
		robot.keyRelease(KeyEvent.VK_LEFT);
		break;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
